export const useMemberStore = defineStore('mine', () => {
  const { requestPost, requestGet, requestGetNew } = useRequest()
  const hasToken = () => process.client && localStorage.getItem('authToken')
  const userEmail = useLocalStorage('userEmail', '')
  const memberInfo = useMyStorage('memberInfo', {})
  const isLogin = computed(() => Object.values(memberInfo.value).length)
  return {
    memberInfo,
    isLogin,
    async updateMemberProfile(user?, utmParams?: object) {
      if (!hasToken()) return
      if (user) return (memberInfo.value = user ?? {})
      const { data } = await requestGet('/app/user/profile', {
        withAuth: true,
        hideError: true,
        params: utmParams,
      })
      memberInfo.value = data ?? {}
      if (memberInfo.value.email && process.client) {
        userEmail.value = memberInfo.value.email
        window.CRISP_TOKEN_ID = memberInfo.value.email
        window?.$crisp?.push(['set', 'user:email', [memberInfo.value.email]])
        window?.$crisp?.push(['do', 'session:reset'])
      }
    },
  }
})
